<template>
  <div class="app-container">
    <el-card class="search-card">
      <el-descriptions title="班级信息">
        <el-descriptions-item label="学院名称">{{ clazzInfo.depName }}</el-descriptions-item>
        <el-descriptions-item label="专业名称"> {{ clazzInfo.subjectName}}</el-descriptions-item>
        <el-descriptions-item label="班级名称"> {{clazzInfo.clazzName}}</el-descriptions-item>
        <el-descriptions-item label="年级">{{ clazzInfo.grade }}</el-descriptions-item>
        <el-descriptions-item label="学制">
          <dict-tag :options="dict.type.educational_system" :value="clazzInfo.educationalSystem"/>
        </el-descriptions-item>
        <el-descriptions-item label="学位类型">
          <dict-tag :options="dict.type.clazz_type" :value="clazzInfo.clazzType"/>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-card class="table-card">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="院系" align="center" prop="depName" />
          <el-table-column label="专业" align="center" prop="subjectName" />
          <el-table-column label="学号" align="center" prop="studentNum" />
          <el-table-column label="姓名" align="center" prop="studentName" />
          <el-table-column label="出生日期" align="center" prop="birth" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="民族" align="center" prop="nation">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.nation" :value="scope.row.nation"/>
            </template>
          </el-table-column>
          <el-table-column label="政治面貌" align="center" prop="politicsStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.politics_status" :value="scope.row.politicsStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="入学时间" align="center" prop="enterTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.enterTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="毕业时间" align="center" prop="graduateTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.graduateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="身份证号" align="center" prop="idCard" />
          <el-table-column label="在校状态" align="center" prop="stateSchool">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.state_school" :value="scope.row.stateSchool"/>
            </template>
          </el-table-column>
          <el-table-column label="年级" align="center" prop="grade" />
          <el-table-column label="班级" align="center" prop="clazzName" />
          <el-table-column label="学制" align="center" prop="educationalSystem">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.educational_system" :value="scope.row.educationalSystem"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" />
          <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
    </el-card>

    <!-- 添加或修改学生信息详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row v-if="form.id">
          <el-col :span="12">
            <el-form-item label="学号" prop="studentNum">
              <el-input v-model="form.studentNum" placeholder="请输入学号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="studentName">
              <el-input v-model="form.studentName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birth">
              <el-date-picker clearable
                              v-model="form.birth"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择出生日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <el-select v-model="form.nation" placeholder="请选择民族">
                <el-option
                  v-for="dict in dict.type.nation"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="政治面貌" prop="politicsStatus">
              <el-radio-group v-model="form.politicsStatus">
                <el-radio
                  v-for="dict in dict.type.politics_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入学时间" prop="enterTime">
              <el-date-picker clearable
                              v-model="form.enterTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择入学时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业时间" prop="graduateTime">
              <el-date-picker clearable
                              v-model="form.graduateTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择毕业时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年级" prop="grade">
              <el-select v-model="form.grade" placeholder="请选择年级" clearable>
                <el-option
                  v-for="g in gradeList"
                  :key="g"
                  :label="g"
                  :value="g"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学制" prop="educationalSystem">
              <el-select v-model="form.educationalSystem" placeholder="请选择学制">
                <el-option
                  v-for="dict in dict.type.educational_system"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在校状态" prop="stateSchool">
              <el-radio-group v-model="form.stateSchool">
                <el-radio
                  v-for="dict in dict.type.state_school"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/information/studentInfo";
import { getClazzInfo, getGradeList } from "@/api/information/clazzInfo";


export default {
  name: "Info",
  dicts: ['educational_system', 'state_school', 'nation', 'politics_status','clazz_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息详情表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clazzId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户Id不能为空", trigger: "blur" }
        ],
        depId: [
          { required: true, message: "院系不能为空", trigger: "blur" }
        ],
        subjectId: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        studentNum: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        studentName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        birth: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        nation: [
          { required: true, message: "民族不能为空", trigger: "change" }
        ],
        politicsStatus: [
          { required: true, message: "政治面貌不能为空", trigger: "change" }
        ],
        enterTime: [
          { required: true, message: "入学时间不能为空", trigger: "blur" }
        ],
        graduateTime: [
          { required: true, message: "毕业时间不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        stateSchool: [
          { required: true, message: "在校状态不能为空", trigger: "change" }
        ],
        grade: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ],
        clazzId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        educationalSystem: [
          { required: true, message: "学制不能为空", trigger: "change" }
        ]
      },
      clazzInfo: {},
      gradeList: []
    };
  },
  created() {
    this.queryParams.clazzId = this.$route.params && this.$route.params.clazzId;
    this.getList();
    this.findClazzInfo(this.queryParams.clazzId);
    this.findGradeList();
  },
  methods: {
    findClazzInfo(clazzId) {
      getClazzInfo(clazzId).then(response => {
        this.clazzInfo = response.data;
      });
    },
    findGradeList(){
      getGradeList().then(response => {
        this.gradeList = response.data;
      })
    },
    /** 查询学生信息详情列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        depId: null,
        subjectId: null,
        studentNum: null,
        studentName: null,
        birth: null,
        nation: null,
        politicsStatus: null,
        enterTime: null,
        graduateTime: null,
        idCard: null,
        stateSchool: null,
        grade: null,
        clazzId: null,
        educationalSystem: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生信息详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.clazzId = this.queryParams.clazzId
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生信息详情编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dews/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
